package syan.label.service.impl;

import cn.hutool.json.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import syan.label.common.HprtPrintItemVO;
import syan.label.common.HprtPrintVO;
import syan.label.entity.dto.LabelPrint;
import syan.label.service.LabelService;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author CHENTAIWEN
 * @create 2022/12/2 11:26
 */
@Primary
@Service
@Slf4j
public class LabelServiceImpl implements LabelService {


    @Override
    public List<HprtPrintVO> createTag(LabelPrint labelPrint) {
        createTag(labelPrint.getEdition(), labelPrint.getList(), labelPrint.getPrintUnit(), labelPrint.getPnQty());
    }

    @Override
    public List<HprtPrintVO> createTag(String edition, List<JSONObject> list, String printUnit, Long pnQty) {

        List<HprtPrintVO> result = Lists.newArrayList();

        List<JSONObject> tempList = Lists.newArrayList();
        int startIndex = 0;
        for (int i = 0; i < list.size(); i++) {
            tempList.add(list.get(i));

            if (i == 8 || (list.size() - 1 == i && list.size() <= 9)) {
                HprtPrintVO tagItem = createTagItem1(pnQty, edition, tempList, printUnit);
                result.add(tagItem);
                tempList = Lists.newArrayList();
                startIndex = 9;
            } else if ((tempList.size() == 15 || list.size() - 1 == i)) {
                HprtPrintVO tagItem = createTagItem2(startIndex, tempList, printUnit);
                result.add(tagItem);
                tempList = Lists.newArrayList();
                startIndex = startIndex + 15;
            }
        }
        return result;
    }

    /**
     * 构建打印机条目
     *
     * @param edition   打印机型号
     *                  <li>  N41BT: 默认值 </li>
     *                  <li> HPRT_N41：黑色打印机</li>
     * @param list      数据列表
     * @param printUnit 打印机偏移系数
     * @return 返回打印机实体
     */
    private HprtPrintVO createTagItem1(Long pnQty, String edition, List<JSONObject> list, String printUnit) {
        // 设置纸张 600mm ❌ 800mm

        HprtPrintVO hprtPrintVO = new HprtPrintVO(0, 600, 800, 1);
        hprtPrintVO.setEdition(null);

        hprtPrintVO.setBold(Boolean.TRUE);
        hprtPrintVO.setClientType(printUnit);

        int startX = 10, startY = 6;
        int rowTextHeight = 5, font = 8;

        JSONObject vo = list.get(0);
        List<HprtPrintItemVO> printItems = Lists.newArrayList();
        createHprtText(printUnit, "装车单", startX, startY, 4, printItems);
        createTableHead(printUnit, startX, startY + 5 * rowTextHeight + 2, font, printItems);


        int itemY = startY + 6 * rowTextHeight + 4, position = 4;

        for (int i = 0; i < list.size(); i++) {
            createHprtText(printUnit, String.format("%02d", i + 1), startX, itemY, font, printItems);
            createHprtText(printUnit, String.valueOf(list.get(i).getJSONObject("")), startX + 10, itemY, font, printItems);
            BigDecimal qty = list.get(i).getBigDecimal("").add(list.get(i).getBigDecimal(""));
            createHprtText(printUnit, String.valueOf(qty), startX + 45, itemY, font, printItems);
            itemY += position;
        }

        List<String> qrList = Lists.newArrayList();
        qrList.add(vo.getStr(""));

        // 二维码 坐标（55，3）
        if (HprtPrintVO.EditionTypeEnum.BM.name().equals(edition)) {
            createBmHprtQrcode(printUnit, 65, 6, String.join(";", qrList), font, printItems);
        } else {
            createHprtQrcode(printUnit, 65, 6, String.join(";", qrList), font, printItems);
        }

        for (HprtPrintItemVO printItemVO : printItems) {
            printItemVO.setSize(null);
        }
        hprtPrintVO.setPrintPageDatas(printItems);

        return hprtPrintVO;
    }

    /**
     * 构建打印机条目
     *
     * @param index
     * @param list      数据列表
     * @param printUnit 打印机偏移系数
     * @return 返回打印机实体
     */
    private HprtPrintVO createTagItem2(int index, List<JSONObject> list, String printUnit) {

        HprtPrintVO hprtPrintVO = new HprtPrintVO(0, 600, 800, 1);
        hprtPrintVO.setEdition(null);
        hprtPrintVO.setBold(Boolean.TRUE);
        hprtPrintVO.setClientType(printUnit);

        int startX = 10, startY = 6;
        int rowTextHeight = 5, font = 8;

        List<HprtPrintItemVO> printItems = Lists.newArrayList();

        createTableHead(printUnit, startX, startY, font, printItems);

        int itemY = startY + rowTextHeight + 2, position = 4;

        for (int i = 0; i < list.size(); i++) {
            createHprtText(printUnit, String.format("%02d", i + 1), startX, itemY, font, printItems);
            createHprtText(printUnit, String.valueOf(list.get(i).getJSONObject("")), startX + 10, itemY, font, printItems);
            BigDecimal qty = list.get(i).getBigDecimal("").add(list.get(i).getBigDecimal(""));
            createHprtText(printUnit, String.valueOf(qty), startX + 45, itemY, font, printItems);
            itemY += position;
        }

        for (HprtPrintItemVO printItemVO : printItems) {
            printItemVO.setSize(null);
        }
        hprtPrintVO.setPrintPageDatas(printItems);

        return hprtPrintVO;
    }

    private void createTableHead(String printUnit, int x, int y, Integer font, List<HprtPrintItemVO> printItems) {
        String line = "—————————————————————————————";
        createHprtText(printUnit, line, x, y, font, printItems);
        int titleY = y + 2;
        createHprtText(printUnit, "#", x, titleY, font, printItems);
        createHprtText(printUnit, "Pallet", x + 10, titleY, font, printItems);
        createHprtText(printUnit, "PartQTY", x + 45, titleY, font, printItems);
        // 坐标（3,43）
        createHprtText(printUnit, line, x, titleY + 2, font, printItems);
    }

    private void createHprtText(String printUnit, String data, int x, int y, Integer font, List<HprtPrintItemVO> printItems) {
        HprtPrintItemVO hprtPrintItemVO = new HprtPrintItemVO(HprtPrintItemVO.PrintTypeEnum.TEXT, HprtPrintItemVO.CommandTypeEnum.TEXT, HprtPrintItemVO.AlignTypeEnum.LEFT, x, y, font, Boolean.TRUE, data);

        hprtPrintItemVO.setPrintUnit(printUnit);

        printItems.add(hprtPrintItemVO);
    }

    /**
     * 生成二维码
     *
     * @param x          横坐标
     * @param y          纵坐标
     * @param font       字体
     * @param printItems 收集器
     */
    private void createBmHprtQrcode(String printUnit, int x, int y, String data, Integer font, List<HprtPrintItemVO> printItems) {
        HprtPrintItemVO palletCode = new HprtPrintItemVO(HprtPrintItemVO.PrintTypeEnum.QRCODE, HprtPrintItemVO.CommandTypeEnum.BARCODE, HprtPrintItemVO.AlignTypeEnum.LEFT, x, y, font, data);
        palletCode.setPrintUnit(printUnit);
        palletCode.setQrCodeWidth(6);
        palletCode.setQrUnit("9");
        printItems.add(palletCode);
    }

    /**
     * 生成二维码
     *
     * @param x          横坐标
     * @param y          纵坐标
     * @param font       字体
     * @param printItems 收集器
     */
    private void createHprtQrcode(String printUnit, int x, int y, String data, Integer font, List<HprtPrintItemVO> printItems) {
        HprtPrintItemVO palletCode = new HprtPrintItemVO(HprtPrintItemVO.PrintTypeEnum.QRCODE, HprtPrintItemVO.CommandTypeEnum.BARCODE, HprtPrintItemVO.AlignTypeEnum.RIGHT, x, y, font, data);
        palletCode.setPrintUnit(printUnit);
        palletCode.setQrCodeWidth(6);
        palletCode.setQrUnit("9");
        printItems.add(palletCode);
    }
}
